package cn.tedu.qxhouserent.house.service.impl;

import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.commons.util.PageInfoToPageDataConverter;
import cn.tedu.qxhouserent.commons.ex.ServiceException;
import cn.tedu.qxhouserent.commons.web.ServiceCode;
import cn.tedu.qxhouserent.house.mapper.NewsCategoryMapper;
import cn.tedu.qxhouserent.house.pojo.vo.NewsCategoryListItemVO;
import cn.tedu.qxhouserent.house.pojo.vo.NewsCategoryStandardVO;
import cn.tedu.qxhouserent.house.pojo.vo.NewsListVO;
import cn.tedu.qxhouserent.house.service.NewsCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class NewsCategoryServiceImpl implements NewsCategoryService {
    @Autowired
    NewsCategoryMapper newsCategoryMapper;

    @Override
    public List<NewsCategoryListItemVO> list() {
        log.debug("开始处理【查询资讯分类列表】的业务！");
        List<NewsCategoryListItemVO> list = newsCategoryMapper.list();
        log.debug("处理【查询资讯分类列表】的业务完成！");
        return list;
    }

    @Override
    public List<NewsListVO> cha(Long id) {


        List<NewsListVO> newsListVO = newsCategoryMapper.cha(id);
        for (NewsListVO n:newsListVO){
            String s = newsCategoryMapper.cha1(n.getId());
            n.setContent(s);
        }
        return newsListVO;
    }

    //根据ID查询分类资讯详情
    @Override
    public NewsCategoryStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询分类资讯详情】的业务，参数：{}", id);
        NewsCategoryStandardVO queryResult = newsCategoryMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询分类资讯详情失败，数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<NewsListVO> listCategoryByUserId(Long id, Integer page) {
        Integer pageSize = 5;
        return listCategoryByUserId(id, page, pageSize);
    }

    @Override
    public PageData<NewsListVO> listMoChaByUId(Long id, Integer page, String s) {
        Integer pageSize = 5;
        return listMoCha(id, page, pageSize, -1, s);
    }

    @Override
    public PageData<NewsListVO> listMoChaByZId(Integer page, Integer id, String s) {
        Integer pageSize = 5;
        if (id == null) id = -1;
        return listMoCha(-1l, page, pageSize, id, s);
    }

    private PageData<NewsListVO> listMoCha(Long id, Integer page, Integer pageSize, Integer zid ,String s) {
        log.debug("开始处理【根据用户id查询房源列表】的业务，用户id: {},页码：{}，每页记录数：{},咨询id:{}", id, page, pageSize,zid);
        PageHelper.startPage(page, pageSize);
        List<NewsListVO> list = newsCategoryMapper.listMoCha(id, zid, s);
        PageInfo<NewsListVO> pageInfo = new PageInfo<>(list);
        PageData<NewsListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

    private PageData<NewsListVO> listCategoryByUserId(Long id, Integer page, Integer pageSize) {
        log.debug("开始处理【根据用户id查询房源列表】的业务，用户id: {},页码：{}，每页记录数：{}", id, page, pageSize);
        PageHelper.startPage(page, pageSize);
        List<NewsListVO> list = newsCategoryMapper.listCategoryByUserId(id);
        PageInfo<NewsListVO> pageInfo = new PageInfo<>(list);
        PageData<NewsListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }
}
